import axios from 'axios'
import { Toast } from 'vant'
import store from '@/store'
import router from '@/router'
import qs from 'qs'

// let baseURL = process.env.NODE_ENV === 'development' ? 'http://192.168.40.239:8080' : 'https://job.chnnice.com'
let baseURL = 'https://job.chnnice.com'
    // create an axios instance
let loadingInstance = null
const service = axios.create({
    // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    baseURL: baseURL,
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 90000 // request timeout
})

// request interceptor 请求拦截
service.interceptors.request.use(

    config => {
        // do something before request is sent
        // if (store.getters.token) {
        //   config.headers['X-Token'] = getToken()
        // }
        // if(config.method === 'post'){
        //   config.data = qs.stringify(config.data)
        // }
        const nice_phone = localStorage.getItem('nice_phone')
        if (nice_phone) {
            config.headers['Token'] = nice_phone //请求携带 token
        }

        loadingInstance = Toast.loading({ message: '加载中...', duration: 0, forbidClick: true, loadingType: 'spinner' }) //loading

        return config
            // return config
    },
    error => {
        // do something with request error
        loadingInstance.clear();
        console.log('request', error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor  响应拦截
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        const res = response.data
        loadingInstance.clear();
        if (res.code === 401) {
            Toast.fail(`当前登陆过期，请重新登录`)
            localStorage.removeItem('nice_phone')
            router.replace('/nice-login')
            return {}
        }
        if (res.code === 0) {
            return res
        } else {
            Toast.fail(`${res.msg}`)
            return {}
        }
        // if the custom code is not 20000, it is judged as an error.
        // if (res.code !== 200) {
        //   // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
        //   if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        //     // to re-login
        //     Toast.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
        //       confirmButtonText: 'Re-Login',
        //       cancelButtonText: 'Cancel',
        //       type: 'warning'
        //     }).then(() => {
        //       store.dispatch('user/resetToken').then(() => {
        //         location.reload()
        //       })
        //     })
        //   }
        //   return Promise.reject(new Error(res.message || 'Error'))
        // } else {
        //   return res
        // }


    },
    error => {
        loadingInstance.clear();
        console.log('response' + error) // for debug
        Toast.fail({
            message: error.message,
            duration: 1.5 * 1000
        })
        return Promise.reject(error)
    }
)

export default service